
#ifndef RTMP_INCLUDE_RESOURCE_H_
#define RTMP_INCLUDE_RESOURCE_H_

#include <map>
#include <memory>
#include <set>
#include <vector>
#ifndef RTMP_INCLUDE_MESSAGE_H_
#include "rtmp_message.h"
#endif

class RtmpConnection;
bool RtmpConnectionCompare(std::shared_ptr<RtmpConnection> & a, std::shared_ptr<RtmpConnection> & b);

class RtmpMediaResource
{
public:
    RtmpMediaResource();
    ~RtmpMediaResource();
    void UpdateStream(const std::string & stream);
    void ProcessMessage(std::shared_ptr<RtmpConnection> conn, std::shared_ptr<RtmpMessage> & msg);
    void BroadcastMsg(std::shared_ptr<RtmpMessage> msg);
    void TakePushConn(std::shared_ptr<RtmpConnection> conn);
    void AddPullConn(std::shared_ptr<RtmpConnection> conn);
    bool CheckPushConn(std::shared_ptr<RtmpConnection> conn);
    void RemoveConn(std::shared_ptr<RtmpConnection> conn);
    void SaveMetadData(std::shared_ptr<RtmpMessage> & msg);
private:
    std::shared_ptr<RtmpMessage> m_meta_data;
private:
    std::string m_app;
    std::string m_stream;
private:
    std::shared_ptr<RtmpConnection> m_push_conn;
    std::set<std::shared_ptr<RtmpConnection>>    m_pull_conn_set;
};

class RtmpMediaResourceMgr
{
public:
    static RtmpMediaResourceMgr * Instance()
    {
        static RtmpMediaResourceMgr mgr;
        return &mgr; 
    }
    std::shared_ptr<RtmpMediaResource> Source(const std::string & stream);
    void RemoveResource(const std::string & stream);
private:
    RtmpMediaResourceMgr();
private:
    std::map<std::string, std::shared_ptr<RtmpMediaResource>> m_media_source_map;
    /* std::map<std::string, std::set<std::string, std::shared_ptr<RtmpMediaResource>>> m_media_map; */
};

#endif
